package com.spic.business.alarm.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil
{
    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static Date strToDate(String dateTimeStr, String formatStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date, String formatStr) {
        if (date == null)
            return " ";
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    public static Date strToDate(String dateTimeStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date) {
        if (date == null)
            return " ";
        DateTime dateTime = new DateTime(date);
        return dateTime.toString("yyyy-MM-dd HH:mm:ss");
    }
    //判断当前时间是否是本月最后一天
    public static boolean isMonthEndDate() {
        LocalDateTime currentTime = LocalDateTime.now();
        // 获取当前年月日
        LocalDate localDate = LocalDateTime.now().toLocalDate();
        // 获取最后一天的最小时间
        LocalDateTime minTime = LocalDateTime.of(localDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MIN);
        // 获取最后一天的最大时间
        LocalDateTime maxTime = LocalDateTime.of(localDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
        if (currentTime.isAfter(minTime) && currentTime.isBefore(maxTime)) {
            return true; //今天是本月最后一天
        } else {
            return false; //天不是本月最后一天
        }
    }
    //判断当前时间是否是第一天
    public static boolean isMonthBeginDate() {
        LocalDateTime currentTime = LocalDateTime.now();
        // 获取当前年月日
        LocalDate localDate = LocalDateTime.now().toLocalDate();
        // 获取第一天的最小时间
        LocalDateTime minTime = LocalDateTime.of(localDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
        // 获取第一天的最大时间
        LocalDateTime maxTime = LocalDateTime.of(localDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MAX);
        if (currentTime.isAfter(minTime) && currentTime.isBefore(maxTime)) {
            return true; //今天是本月第一天
        } else {
            return false; //天不是本月第一天
        }
    }

    /**
     * 取当前月的上一个 年月份
     * @param date
     * @return
     */
    public static String getLastDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH,-1);
        cal.getTime();
        if (date == null)
            return " ";
        DateTime dateTime = new DateTime(cal.getTime());
        return dateTime.toString("yyyyMM");
    }

    public static void main(String[] args) {
        System.out.println(dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        System.out.println(strToDate("2010-01-01 11:11:11", "yyyy-MM-dd HH:mm:ss"));
        System.out.println("--------");
                System.out.println(dateToStr(new Date()));
        System.out.println(strToDate("2019-04-10 08:53:05"));
    }
}

